<?php

Raise::load('db.core.RaiseDbRelationship');
Raise::load('core.RaiseCollection');

/**
 * RaiseDbManyToMany class
 * Defines a many to many relationship
 * A <- J -> B; Junction table J has one A and one B; One A can have many B, and one B can have many A.
 *
 * @author Sam Yong
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package phpraise.db.relationship
 * @since 1.2
 */
class RaiseDbManyToMany extends RaiseDbRelationship {
        
    /**
     * The relation that relates to the this model through the junction relation
     * @var string
     */
    private $model;
    
    /**
     * The junction relation that this model is related to
     * @var string
     */
    private $junction;
    
    /**
     * The foreign key that identifies the this model in the junction model
     * @var RaiseCollection|string
     */
    private $foreignKey;
    
    /**
     * The foreign key that identifies the target model in the junction model
     * @var RaiseCollection|string
     */
    private $targetForeignKey;
    
    /**
     * Create a new RaiseDbManyToMany relationship
     * @param string $junction
     * @param string|RaiseDbModel $model
     * @param RaiseCollection|string $foreignKey
     * @param RaiseCollection|string $targetForeignKey 
     */
    public function __construct($junction, $model, $foreignKey = null, $targetForeignKey = null){
        $this->junction($junction);
        $this->model($model);
        $this->foreignKey($foreignKey);
        $this->targetForeignKey($targetForeignKey);
    }
    
    /**
     * Get or set the relation that relates to the junction model
     * @param string $m (optional) Set the relation that relates to the junction model
     * @return string
     */
    public function model($m = null){
        if(func_num_args() == 1){
            $this->model = $m;
        }
        return $this->model;
    }

    /**
     * Get or set the junction relation that this model is related to
     * @param string $j (optional) Set the junction relation that this model is related to
     * @return string
     */
    public function junction($j = null){
        if(func_num_args() == 1){
            $this->junction = $j;
        }
        return $this->junction;
    }

    /**
     * Get or set the foreign key that identifies the this model in the junction model
     * @param RaiseCollection|string $f (optional) Set the foreign key that identifies the this model in the junction model
     * @return RaiseCollection|string
     */
    public function foreignKey($f = null){
        if(func_num_args() == 1){
            $this->foreignKey = $f;
        }
        return $this->foreignKey;
    }

    /**
     * Get or set the foreign key that identifies the target model in the junction model
     * @param RaiseCollection|string $t (optional) Set the foreign key that identifies the target model in the junction model
     * @return RaiseCollection|string
     */
    public function targetForeignKey($t = null){
        if(func_num_args() == 1){
            $this->targetForeignKey = $t;
        }
        return $this->targetForeignKey;
    }
    
}
